package com.example.elmmerchants.Product_Management;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;

import java.util.ArrayList;
import java.util.List;

public class Update_Specification_Page extends AppCompatActivity {

    LinearLayout specification_item, add_the_specification, specification_information, is_specification, current;
    TextView current_text_view, delete_all_specification;

    ArrayList<String> specification = new ArrayList<>();
    List<Specification_List> specification_lists = new ArrayList<>();
    int how_many_specification = 0;
    AlertDialog dialog;
    boolean is_all_input = true;

    private class Loading_Specification_Thread extends Thread {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < specification.size(); i += 6) {
                        current.setBackgroundResource(R.drawable.border_yellow);
                        current_text_view.setTextColor(Color.BLACK);
                        Specification_List s = new Specification_List();
                        how_many_specification += 1;
                        is_specification.setVisibility(View.VISIBLE);

                        View view2 = LayoutInflater.from(Update_Specification_Page.this).inflate(R.layout.specification_information_view, null);
                        TextView specification_name = view2.findViewById(R.id.specification_name);
                        specification_name.setText(specification.get(i));
                        EditText set_product_price = view2.findViewById(R.id.set_product_price);
                        set_product_price.setText(specification.get(i + 1));
                        EditText set_inventory = view2.findViewById(R.id.set_inventory);
                        set_inventory.setText(specification.get(i + 2));
                        Switch is_open_auto_re_inventory = view2.findViewById(R.id.is_open_auto_re_inventory);
                        if (specification.get(i + 3).equals("1")) {
                            is_open_auto_re_inventory.setChecked(true);
                        }
                        else {
                            is_open_auto_re_inventory.setChecked(false);
                        }
                        Switch is_sold = view2.findViewById(R.id.is_sold);
                        if (specification.get(i + 4).equals("1")) {
                            is_sold.setChecked(true);
                        }
                        else {
                            is_sold.setChecked(false);
                        }
                        EditText weight = view2.findViewById(R.id.weight);
                        weight.setText(specification.get(i + 5));
                        specification_information.addView(view2);

                        View view1 = LayoutInflater.from(Update_Specification_Page.this).inflate(R.layout.set_specification_view, null);
                        EditText specification_name_edit_text = view1.findViewById(R.id.specification_name);
                        specification_name_edit_text.setText(specification.get(i));
                        specification_name_edit_text.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                            }

                            @Override
                            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                            }

                            @Override
                            public void afterTextChanged(Editable editable) {
                                specification_name.setText(specification_name_edit_text.getText().toString());
                            }
                        });
                        ImageView delete = view1.findViewById(R.id.delete);
                        delete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                how_many_specification -= 1;
                                specification_item.removeView(view1);
                                for (int i = 0; i < specification_lists.size(); i++) {
                                    if (view1 == specification_lists.get(i).view) {
                                        specification_lists.remove(i);
                                        break;
                                    }
                                }

                                specification_information.removeView(view2);
                                if (how_many_specification == 0) {
                                    is_specification.setVisibility(View.GONE);
                                    current.setBackgroundResource(R.drawable.border_gray_10dp);
                                    current_text_view.setTextColor(Color.rgb(128, 128, 128));
                                }
                            }
                        });
                        specification_item.addView(view1);
                        s.view = view1;
                        s.is_sold = is_sold;
                        s.set_specification_name = specification_name_edit_text;
                        s.set_inventory = set_inventory;
                        s.set_product_price = set_product_price;
                        s.is_open_auto_re_inventory = is_open_auto_re_inventory;
                        s.weight = weight;
                        specification_lists.add(s);
                    }
                    dialog.dismiss();
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_specification_page);

        specification = getIntent().getStringArrayListExtra("specification");

        specification_item = findViewById(R.id.specification_item);
        add_the_specification = findViewById(R.id.add_the_specification);
        specification_information = findViewById(R.id.specification_information);
        is_specification = findViewById(R.id.is_specification);
        current = findViewById(R.id.current);
        current_text_view = findViewById(R.id.current_text_view);
        delete_all_specification = findViewById(R.id.delete_all_specification);

        set_dialog();
        Loading_Specification_Thread loadingSpecificationThread = new Loading_Specification_Thread();
        loadingSpecificationThread.start();

        delete_all_specification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                builder.setCancelable(false);
                builder.setTitle("是否清空");
                builder.setMessage("是否清空所有规格");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        specification.clear();
                        Intent intent = new Intent(Update_Specification_Page.this, Update_Product_Data_Page.class);
                        intent.putStringArrayListExtra("specification_data", specification);
                        setResult(1, intent);
                        finish();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                builder.create();
                builder.show();
            }
        });

        add_the_specification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                current.setBackgroundResource(R.drawable.border_yellow);
                current_text_view.setTextColor(Color.BLACK);
                Specification_List s = new Specification_List();
                how_many_specification += 1;
                is_specification.setVisibility(View.VISIBLE);

                View view2 = LayoutInflater.from(Update_Specification_Page.this).inflate(R.layout.specification_information_view, null);
                TextView specification_name = view2.findViewById(R.id.specification_name);
                EditText set_product_price = view2.findViewById(R.id.set_product_price);
                EditText set_inventory = view2.findViewById(R.id.set_inventory);
                Switch is_open_auto_re_inventory = view2.findViewById(R.id.is_open_auto_re_inventory);
                Switch is_sold = view2.findViewById(R.id.is_sold);
                EditText weight = view2.findViewById(R.id.weight);
                specification_information.addView(view2);

                View view1 = LayoutInflater.from(Update_Specification_Page.this).inflate(R.layout.set_specification_view, null);
                EditText specification_name_edit_text = view1.findViewById(R.id.specification_name);
                specification_name_edit_text.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        specification_name.setText(specification_name_edit_text.getText().toString());
                    }
                });
                ImageView delete = view1.findViewById(R.id.delete);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        how_many_specification -= 1;
                        specification_item.removeView(view1);
                        for (int i = 0; i < specification_lists.size(); i++) {
                            if (view1 == specification_lists.get(i).view) {
                                specification_lists.remove(i);
                                break;
                            }
                        }

                        specification_information.removeView(view2);
                        if (how_many_specification == 0) {
                            is_specification.setVisibility(View.GONE);
                            current.setBackgroundResource(R.drawable.border_gray_10dp);
                            current_text_view.setTextColor(Color.rgb(128, 128, 128));
                        }
                    }
                });
                specification_item.addView(view1);
                s.view = view1;
                s.is_sold = is_sold;
                s.set_specification_name = specification_name_edit_text;
                s.set_inventory = set_inventory;
                s.set_product_price = set_product_price;
                s.is_open_auto_re_inventory = is_open_auto_re_inventory;
                s.weight = weight;
                specification_lists.add(s);
            }
        });

        current.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (specification_lists.size() > 0) {
                    is_all_input = true;
                    specification.clear();
                    for (int i = 0; i < specification_lists.size(); i++) {
                        if (specification_lists.get(i).set_specification_name.getText().toString().isEmpty() || specification_lists.get(i).set_product_price.getText().toString().isEmpty() || specification_lists.get(i).set_inventory.getText().toString().isEmpty() || specification_lists.get(i).weight.getText().toString().isEmpty()) {
                            is_all_input = false;
                            break;
                        }
                        specification.add(specification_lists.get(i).set_specification_name.getText().toString());
                        specification.add(specification_lists.get(i).set_product_price.getText().toString());
                        specification.add(specification_lists.get(i).set_inventory.getText().toString());
                        if (specification_lists.get(i).is_open_auto_re_inventory.isChecked()) {
                            specification.add("1");
                        }
                        else {
                            specification.add("0");
                        }
                        if (specification_lists.get(i).is_sold.isChecked()) {
                            specification.add("1");
                        }
                        else {
                            specification.add("0");
                        }
                        specification.add(specification_lists.get(i).weight.getText().toString());
                    }

                    if (is_all_input) {
                        Intent intent = new Intent(Update_Specification_Page.this, Update_Product_Data_Page.class);
                        intent.putStringArrayListExtra("specification_data", specification);
                        setResult(1, intent);
                        finish();
                    }
                    else {
                        Toast.makeText(Update_Specification_Page.this, "请输入所有数据", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(Update_Specification_Page.this, "请设置规格", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}